/**
 * Class simplifying interaction with SQL server
 * This is a singleton to allow a single connection to 
 * the database to be used (optimizing connections)
 * NOTE : DO NOT USE IN FINAL VERSION, MUST BE OPTIMIZED FIRST
 * @author : bigredshoes03
 * @updated : 9/30/09
 */
package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQL{
	private static SQL unique;
	private Connection conn;
	
	/**
	 * Constructor, handles connection to database and registration
	 * with driver manager
	 * This should be updated to handle dynamic locations/user:pass combos
	 */
	public SQL(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost/pewpew?user=root&password=");
		} catch (ClassNotFoundException e) {
		} catch (SQLException e) {}
	}
	
	/**
	 * Make it a thread-safe singleton
	 * @return - SQL instance
	 */
	public static SQL getInstance(){
		if(unique==null){
			synchronized(new Object()){
				if(unique==null){
					unique = new SQL();
				}
			}
		}
		return unique;
	}
	
	/**
	 * Query the table, return a result set
	 * based on query. 
	 * @param query - query to perform
	 * @return ResultSet
	 * @return null if no return
	 */
	public ResultSet query(String query){
		Statement state;
		ResultSet ret = null;
		try {
			state = conn.createStatement();
			ret = state.executeQuery(query);
		} catch (SQLException e) {}
		return ret;
	}
}
